import { observable, action } from 'mobx';
import { Toast, Bubble, Tools } from 'weaver-mobile';
import * as Apis from './../../api/remind'
import { Reply, BrowserHrm, Browser } from 'weaver-mobile-page';
import moment from 'moment';
const getLabel = Tools.getLabel;
export default class RemindAddFormStore {
    comObj = {};
    params = {};
    @observable isSuccess = false;
    @observable loading = true;
    @observable msgCode = "";
    // @observable form = {};
    @observable currentPath = {};

    @observable receiver = "";
    @observable receiverBroswer = {};
    @observable receiverBroswerValue = [];//接收人

    @observable sellChanceId = "";
    @observable chanceBroswer = {};
    @observable chanceBroswerValue = [];//商机

    @observable customerid = "";
    @observable customerBroswer = {};
    @observable customerBroswerValue = [];//客户

    @observable contacterid = "";
    @observable contacterBroswer = {};
    @observable contacterBroswerValue = [];//联系人

    @observable sendWay = 1;//发送方式,1:应用内；2：短信
    @observable timedSende = false;//定时发送
    @observable senderTime = "";//定时发送时间
    @observable textareaValue = "";//输入框的值
    @observable reply = new Reply.Store();
    @observable commonLanguageList = [];
    @observable loading1 = true;//常用语联想加载
    @observable isSuccess1 = false;//常用语联想加载
    @observable selectedIndex = 0;
    @action
    doNew = (comObj) => {
        this.comObj = comObj;
        // this.doInit();
        this.currentPath = comObj.props.location.pathname;
        // this.form = new Form.Store({ route: this.currentPath });
        this.receiverBroswer = new BrowserHrm.Store({ route: this.currentPath, id: 'receiverLayout' });
        this.chanceBroswer = new Browser.Store({ route: this.currentPath, id: 'chanceLayout' });
        this.customerBroswer = new Browser.Store({ route: this.currentPath, id: 'customerLayout' });
        this.contacterBroswer = new Browser.Store({ route: this.currentPath, id: 'contacterLayout' });

        const now = moment().format('YYYY-MM-DD HH:MM');
        // console.log(now)
        this.senderTime = now;
        if (comObj.props.params) {
            const p = comObj.props.params;
            if (p.receiver) {
                this.receiverBroswerValue = [{ id: p.receiver, name: p.receiverspan }];
                this.receiver = p.receiver;
            }
            if (p.sellchanceid) {
                this.chanceBroswerValue = [{ id: p.sellchanceid, name: p.sellchancespan }];
                this.sellChanceId = p.sellchanceid;
            }
        }
        this.getCommonLanguageList();
    }

    @action
    setState = (params = {}) => {
        Object.keys(params).forEach((key) => {
            // console.log(params)
            this[key] = params[key];
            // console.log(this[key])
            this.getBroswerString(key)
        });
    }

    //浏览按钮选择完后，将值处理成字符串
    getBroswerString = (key) => {
        let p = [];
        this[key].forEach((key) => {
            p.push(key.id);
        })
        if (key == "receiverBroswerValue") {
            this.receiver = `${p}`;
        } else if (key == "chanceBroswerValue") {
            this.sellChanceId = `${p}`;
        } else if (key == "customerBroswerValue") {
            this.customerid = `${p}`;
        } else if (key == "contacterBroswerValue") {
            this.contacterid = `${p}`;
        }
    }

    /**
     * 保存提醒
     */
    @action
    saveAddRemind = () => {
        //如果输入框内容为空，则提示
        if (this.textareaValue == "") {
            Bubble.error(getLabel('514792', '提醒内容不能为空'), 2);
            return;
        }
        //如果接收人内容为空，则提示
        if (this.receiver == "") {
            Bubble.error(getLabel('514793', '接收人不能为空'), 2);
            return;
        }
        let now = moment().format('YYYY-MM-DD HH:mm:ss');
        if (this.timedSende) {
            const date3 = moment(this.senderTime).diff(now, 'minute');//计算相差的分钟数
            if (date3 < 0) {
                Bubble.error(getLabel('515270', '定时发送时间不能小于当前时间'));
                return;
            }
        }
        let type = "";
        if (this.sellChanceId == "") {
            type = "2";
        } else {
            type = "0";
        }
        let params = {
            content: this.textareaValue, receiver: this.receiver, sellchanceid: this.sellChanceId, type: type,
            sendWay: this.sendWay, senderTime: this.senderTime, timedSende: this.timedSende, customer: this.customerid, contacterid: this.contacterid
        }
        // console.log(params);
        // if (this.form.doValidate().pass) {
        Apis.saveRemind(params).then(data => {
            if (data.status == "success") {
                Bubble.success(getLabel('514794', '提醒成功'), 2);
                this.comObj.props.callBack && this.comObj.props.callBack();
            } else {
                Toast.fail(data.msgCode, 1);
            }
            this.comObj.props.history.go(-1);
        })
        // }
    }

    //是否定时发送
    @action
    timedSendeChange = (value) => {
        this.timedSende = value;
    }
    //发送方式切换
    @action
    sendWayChange = (value) => {
        this.selectedIndex = value;
        if (value == "1") {
            this.sendWay = 2;
        } else if (value == "0") {
            this.sendWay = 1;
        }
    }
    //日期-时间选择
    @action
    dataPickChange = (value) => {
        // console.log(value)
        this.senderTime = value;
    }

    //改变输入框的值
    @action
    textareaValueChange = (value) => {
        // console.log(value)
        this.textareaValue = value;
        // this.getCommonLanguageList();
    }
    //获取快捷输入的内容
    @action
    getCommonLanguageList = () => {
        this.loading1 = true;
        this.isSuccess1 = false;
        Apis.getCommonLanguageList({ content: this.textareaValue }).then(data => {
            this.commonLanguageList = data.commonLanguageList;
            // console.log(data.status);
            this.isSuccess1 = true;
            this.loading1 = false;
        });

    }
    //将常用语追加至光标所在位置
    @action
    commonLanguage = () => {

    }

    //添加常用回复
    @action
    submitCommomReply = (datas) => {
        // console.log(toJS(datas));
        let params = { type: 1, text: this.removeHtmlTab(datas.text) }
        Apis.saveCommonLanguage(params).then(data => {
            if (data.status == "success") {
                Bubble.success(getLabel('514796', '常用语保存成功'), 1);
                this.getCommonLanguageList();
            }
        })
        this.reply.doClear();
        // 2、设置visible为false
        const replyRef = this.comObj.replyRef; // 避免某一瞬间找不到this.replyRef
        if (replyRef) {
            replyRef.setVisible(false);
        }
    }
    //去掉HTML标签
    removeHtmlTab = (tab) => {
        return tab.replace(/<[^<>]+?>/g, '');//删除所有HTML标签
    }
    //评论的点击次数加一
    @action
    saveCommonLanguageCount = (params) => {
        Apis.saveCommonLanguageCount(params).then(data => {
            // console.log(data)
        });
    }
    //删除常用语
    @action
    deleteCommonLanguage = (params) => {
        Apis.deleteCommonLanguage(params).then(data => {
            this.getCommonLanguageList();
        });
    }
    @action
    resetForm = () => {
        this.textareaValue = "";
        this.sendWay = 1;
        this.timedSende = false;
        this.senderTime = moment().format('YYYY-MM-DD HH:MM');
        this.chanceBroswerValue = [{ id: '', name: '' }];
        this.receiverBroswerValue = [{ id: '', name: '' }];
        this.receiver = "";
        this.sellChanceId = "";
        this.customerBroswerValue = [{ id: '', name: '' }];
        this.contacterBroswerValue = [{ id: '', name: '' }];
        this.contacterid = "";
        this.customerid = "";
    }
}